package com.example.dell.intelligentagriculture.fragment;


import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.example.dell.intelligentagriculture.DataControlActivity;
import com.example.dell.intelligentagriculture.MainActivity;
import com.example.dell.intelligentagriculture.R;
import com.example.dell.intelligentagriculture.utils.HttpUtil;
import com.example.dell.intelligentagriculture.utils.MyApp;
import java.io.IOException;

public class SetFragment extends Fragment implements View.OnClickListener {
    LinearLayout aboutUsContent;
    RelativeLayout control, cache, updateVersions, aboutUs;
    TextView version;
    private float mDensity;
    private int mHiddenViewMeasuredHeight;
    View view;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_set, null);
            mDensity = getResources().getDisplayMetrics().density;
            mHiddenViewMeasuredHeight = (int) (mDensity * 160 + 0.5);
            initView(view);
        }
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null) {
            parent.removeView(view);
        }
        return view;
    }
    private void initView(View view) {
        control = view.findViewById(R.id.control);
        cache = view.findViewById(R.id.clearCache);
        updateVersions = view.findViewById(R.id.updateVersion);
        aboutUs = view.findViewById(R.id.aboutUs);
        aboutUsContent = view.findViewById(R.id.aboutUsContent);
        version = view.findViewById(R.id.version);

        //获取当前应用的版本号
        //主要管理应用程序的安装包（apk），可以获取安装包中的信息
        PackageManager manager = getActivity().getPackageManager();
        try {
            // 获取应用程序的信息   getPackageName()获取应用程序的包名
            PackageInfo info = manager.getPackageInfo(getActivity().getPackageName(), 0);
            String versionName = info.versionName;
            version.setText(versionName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        aboutUs = view.findViewById(R.id.aboutUs);
        control.setOnClickListener(this);
        cache.setOnClickListener(this);
        updateVersions.setOnClickListener(this);
        aboutUs.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.control:
                Intent intent = new Intent(getActivity(), DataControlActivity.class);
                MyApp myApp=  ((MainActivity)getActivity()).myApp;
                startActivity(intent);
                break;
            case R.id.clearCache:
                //gc() jdk的垃圾回收机制
                System.gc();
                Toast.makeText(getActivity(), "处于最佳状态", Toast.LENGTH_SHORT).show();

                new Thread() {
                    @Override
                    public void run() {
                        HttpUtil util = new HttpUtil();
                        try {
                            MyApp myApp = (MyApp) getActivity().getApplication();
                            // String data = util.connectServer("admin", "http://192.168.100.100:8890/type/jason/action/getSensor");
                            String data = util.connectServer("admin", myApp, "getSensor");
                            Log.e("onClick: ", data);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }.start();


                break;
            case R.id.updateVersion:
                Toast.makeText(getActivity(), "已经是最新版本", Toast.LENGTH_SHORT).show();
                break;
            case R.id.aboutUs:
                if (aboutUsContent.getVisibility() == View.GONE) {
                    animateOpen(aboutUsContent);
                } else {
                    animateClose(aboutUsContent);
                }
                break;
        }
    }

    private void animateOpen(View v) {
        v.setVisibility(View.VISIBLE);
        ValueAnimator animator = createDropAnimator(v, 0,
                mHiddenViewMeasuredHeight);
        animator.start();
    }

    private void animateClose(final View view) {
        int origHeight = view.getHeight();
        ValueAnimator animator = createDropAnimator(view, origHeight, 0);
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                view.setVisibility(View.GONE);
            }
        });
        animator.start();
    }

    private ValueAnimator createDropAnimator(final View v, int start, int end) {
        ValueAnimator animator = ValueAnimator.ofInt(start, end);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator arg0) {
                int value = (Integer) arg0.getAnimatedValue();

                ViewGroup.LayoutParams layoutParams = v.getLayoutParams();
                layoutParams.height = value;
                v.setLayoutParams(layoutParams);
            }
        });
        return animator;
    }
}
